
require("Utils")
require("SoldierTemplates")
require("Sprite")
require("States")

Bullet = {};

	function Bullet.new(world, owner, dPosition)
		local b = {};
		setmetatable(b, {__index = Bullet});
		
		b.attributes = Templates.newTemplate(BulletTemplate);
		b.origin = BulletTemplate;	
		
		b.sprite = Sprite.newSprite(b, b.origin.sprites.lenX, b.origin.sprites.lenY, true)
		b.sprite:showHPBar(false);
		--b.sprite:drawBoundingBox(true);
		b.sprite:setState(States.WALK)
		
		b.owner = owner;
		b.world = world;
		b.position = {x = owner.center.x - owner.attributes.box.w / 2,  
					  y = owner.center.y - owner.attributes.box.h};
		b.dPosition = dPosition; -- Posição destino		
		b.cPosition = { x = (owner.center.x + dPosition.x) / 2, 
		 			    y =  dPosition.y }
		b.direction = {x = owner.direction.x, y = owner.direction.y}
		b.center = {x = owner.center.x	, y = owner.center.y}
			
		b.speedFactor = b.attributes.speed;		
		
		b.down = false;
		b.active = true;
		
		
		
		b:setDirection(owner.direction.x, owner.direction.y)
		b:updateBoudingBox()
				
		return b;
	end
	
	function Bullet:causeDamage()
		local xmin, xmax = self.center.x - self.attributes.area, self.center.x + self.attributes.area
		for _, units in pairs(self.world.soldiers) do
	        for _, unit in ipairs(units) do
	            if unit.state ~= States.DEAD and unit.center.x > xmin and unit.center.x < xmax then
					BattleManager.processCombatWithBullet(self, unit);
				end
	        end
	    end
	end
	
	function Bullet:updateBoudingBox()
        self.attributes.box.x = self.position.x + self.origin.box.x
        self.attributes.box.y = self.position.y + self.origin.box.y
		self.center.x = self.position.x + (self.sprite.qxSize / 2)
		self.center.y = self.position.y + (self.sprite.qySize / 2)
    end
	
	function Bullet:setDirection(x, y)
        self.sprite:flipAllStateSprites( x > 0, y < 0);
        self.direction.x = x;
        self.direction.y = y;
    end
	
	function Bullet:isActive()
		return self.active;
	end
	
	function Bullet:update(dt)
		self.dt = dt;
		self:updateBoudingBox();
		
		if (self.position.x * self.owner.direction.x) > (self.cPosition.x * self.owner.direction.x) and not self.down then
			self.down = true;
		end
		
		if self:isActive() then
			if ((self.position.y + self.origin.box.h / 2) >= self.dPosition.y) and self.down then
				self.sprite:setState(States.DEAD);
				self.active = false;
				self:causeDamage();
			else
				self:move();
			end
		end
		
		self.sprite:update(dt);
	end
	
	 function Bullet:draw()
		self.sprite:draw(self.position.x, self.position.y)
		-- love.graphics.setColor(0,0,0);
		-- love.graphics.rectangle("fill", self.position.x, self.position.y, 10,10);
	end
	
	function Bullet:move()
		if self.down then
			self.sprite.angle = 1 * self.owner.direction.x;
		else
			self.sprite.angle = -1 * self.owner.direction.x;
		end
		
		if (self.position.x < self.dPosition.x) then 
			self.position.x = self.position.x + self.dt * self.speedFactor; 
		else
			self.position.x = self.position.x - self.dt * self.speedFactor; 	
		end
        if not self.down then 
			self.position.y = self.position.y - self.dt * self.speedFactor / 2; 
		else
			self.position.y = self.position.y + self.dt * self.speedFactor / 2; 
        end
	end
	
